package com.csf.client.decoder;

import java.util.logging.Level;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.handler.ssl.SslHandler;

import com.csf.client.message.TestMessage.Test;
import com.csf.common.utils.Greeter;

public class ProtoBuffDeserialiser extends SimpleChannelUpstreamHandler{

	@Override
	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
		System.out.println("Message received From Server in client:");
		ChannelBuffer cb = ((ChannelBuffer) e.getMessage());
		byte bytes[] = cb.array();
		try {

			Test t = Test.parseFrom(bytes);
			System.out.println("Message : id " + t.getId() + "name"
					+ t.getName());

		}

		catch (Exception exp) {
			exp.printStackTrace();
		}

	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
		e.getCause();
		e.getChannel().close();
	}

//	public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
//			throws Exception {
//		System.out.println("channel connected");
//
//		// Get the SslHandler in the current pipeline.
//		// We added it in SecureChatPipelineFactory.
//		final SslHandler sslHandler = ctx.getPipeline().get(SslHandler.class);
//
//		// Get notified when SSL handshake is done.
//		ChannelFuture handshakeFuture = sslHandler.handshake();
//		handshakeFuture.addListener(new Greeter(sslHandler));
//	}

	
}
